import 'dayjs/locale/zh-cn' // 导入本地化语言
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')

//节流 一段时间内重复触发 减少触发频率
export const throtte = (fn: (...params: any) => any, time: number = 100) => {
  let timer: null | number = null
  return (...args: any[]) => {
    if (timer !== null) return
    timer = window.setTimeout(() => {
      timer = null
      fn(...args)
    }, time)
  }
}

// 展示发布时间 传入时间戳
export const showTime = (timer: string | number): string => {
  const dayTime = 60 * 60 * 24 * 2
  const creationTime = Number(timer)
  if (creationTime === 0) return '0'
  if (!creationTime) throw new Error('timer no number or string,number')
  let showTime = ''
  if (Math.abs(creationTime - new Date().getTime()) > dayTime) {
    showTime = dayjs(creationTime).format('YYYY年MM月DD日')
  } else {
    showTime = dayjs(creationTime).fromNow()
  }
  return showTime
}
//  生成随机数
export const getRandom = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min + 1)) + min
}
export enum PositionType {
  LEFT = 'left',
  TOP = 'top',
}

// 获取元素相对body的位置
export const getElementOffsetPosition = (
  el: HTMLElement | null,
  positonType: PositionType
) => {
  if (el === null) return 0
  let position =
    positonType === PositionType.LEFT ? el.offsetLeft : el.offsetTop
  let parent = el.offsetParent
  while (parent !== null) {
    // parent as HTMLElement
    position += PositionType.LEFT
      ? (parent as HTMLElement).offsetLeft
      : (parent as HTMLElement).offsetTop
    parent = (parent as HTMLElement).offsetParent
  }
  return position
}
export const storage = {
  get(key: string) {
    const valueStr = window.localStorage.getItem(key)
    try {
      return valueStr ? JSON.parse(valueStr) : valueStr
    } catch (error) {
      return valueStr?.toString()
    }
  },
  set(key: string, value: any) {
    let valueStr = value
    if (typeof value === 'object') {
      valueStr = JSON.stringify(value)
    }
    window.localStorage.setItem(key, valueStr)
  },
  remove(key: string) {
    window.localStorage.removeItem(key)
  },
}

// 实现一个页面滚动动画
export const scrollFn = (el: HTMLDivElement, endY: number, time = 500) => {
  let nowY = el.scrollTop
  if (nowY === endY) return
  const isDown = nowY < endY
  const absY = Math.abs(nowY - endY) // 滚动距离的绝对值
  const computTime = absY / time > 4 ? absY / time : 4 // 单位时间
  const timeCount = time / computTime //定时器执行次数
  const loopY = absY / timeCount // 单位距离
  const timer: NodeJS.Timeout = setInterval(() => {
    if ((isDown && nowY + loopY >= endY) || (!isDown && nowY + loopY <= endY)) {
      el.scrollTop = endY
      return clearInterval(timer)
    }
    if (isDown && nowY + loopY < endY) {
      el.scrollTop = nowY += loopY
    } else if (!isDown && nowY + loopY > endY) {
      el.scrollTop = nowY -= loopY
    }
  }, computTime)
  /* // settimeout实现
  const setTimeoutFn = () => {
    const timer: NodeJS.Timeout = setTimeout(() => {
      if (
        (isDown && nowY + loopY > endY) ||
        (!isDown && nowY + loopY < endY)
      ) {
        el.scrollTop = endY
        return clearTimeout(timer)
      }
      if (isDown && nowY + loopY < endY) {
        el.scrollTop = nowY += loopY
      } else if (!isDown && nowY + loopY > endY) {
        el.scrollTop = nowY -= loopY
      }
      setTimeoutFn()
    }, computTime)
  }
  setTimeoutFn() 
  
  */
}

// 简单深拷贝
export const cp = (object: { [k: string]: any } | any[] | null) => {
  if (object === null) return null
  return JSON.parse(JSON.stringify(object))
}

export const isPc = () => {
  return !/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)
}
